package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.ThroughModelAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Classname ThroughModelAttrLinkHandler
 * @Description 穿透模式
 *    值变更时，需连带改变的属性，设为相同值  210008284  IPv4穿透模式
 *                                     210008200  IPv6穿透模式
 * @Author zhang.song
 * @Date 2022-03-14 18:12
 */
@Service
@AttrHandler(ThroughModelAttrHandler.class)
public class ThroughModelAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {


    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        if(Objects.isNull(attr) || KiteStringUtils.isEmpty(attr.getValue())) {
            return;
        }

        String valueDesc = attr.getAttrValues().stream().filter(temp -> KiteStringUtils.isEqual(temp.getAttrValue(), attr.getValue())).findAny().map(KiteAttrValue::getAttrValueName).orElse(null);

        // IPv4穿透模式、IPv6穿透模式
        KiteAttr ip4ThroughModelAttr = inst.getAttr("210008284");
        if(Objects.nonNull(ip4ThroughModelAttr)) {
            ip4ThroughModelAttr.setValue(attr.getValue());
            ip4ThroughModelAttr.setValueDesc(valueDesc);
        }
        KiteAttr ip6ThroughModelAttr = inst.getAttr("210008200");
        if(Objects.nonNull(ip6ThroughModelAttr)) {
            ip6ThroughModelAttr.setValue(attr.getValue());
            ip6ThroughModelAttr.setValueDesc(valueDesc);
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
